package com.tangji.signaltower.controller;

import com.alibaba.fastjson.JSON;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.fqgj.common.api.annotations.ParamsValidate;
import com.tangji.antenna.bean.EventFactory;
import com.tangji.signaltower.runners.WSClient;
import com.tangji.signaltower.runners.client.ForexPoNettyClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: yujianfu (mr.vencnet@gmail.com)
 * Date: 2020-06-02
 * Time: 17:18
 */
@RestController
public class OkController {
    private static final Logger logger = LoggerFactory.getLogger(OkController.class);

    @Autowired
    private RedisClient redisClient;

    @RequestMapping("/ok")
    public String ok() {
        return "ok";
    }


    @RequestMapping("/socket")
    public String socket() {
        try {
            ForexPoNettyClient inst = ForexPoNettyClient.getInst();
            inst.setConnectFlag(false);
            inst.startConnectServerAlways(System.currentTimeMillis() + "");
        } catch (Exception e) {
            logger.error("== oksocket ==", e);
        }
        return "ok";
    }


    @RequestMapping("/redis")
    public String redis() {
        redisClient.set("aaaa", "6666");
        return redisClient.get("aaaa");
    }

    /**
     * list
     *
     * @return
     */
    @RequestMapping(value = "/test/redis/list/{key}/get")
    @ParamsValidate
    public Response redisListGet(@PathVariable String key) {
        List<String> list = (List) redisClient.listRange(key, 0, -1, key);
        return Response.ok(JSON.toJSONString(list));
    }

    /**
     * list size
     *
     * @return
     */
    @RequestMapping(value = "/test/redis/list/{key}/size")
    @ParamsValidate
    public Response redisListSize(@PathVariable String key) {
        Long length = redisClient.listSize(key);
        return Response.ok(JSON.toJSONString(length));
    }

    /**
     * list
     *
     * @return
     */
    @RequestMapping(value = "/test/redis/zset/{key}/get")
    @ParamsValidate
    public Response redisZsetGet(@PathVariable String key) {
        Set<String> list = (Set) redisClient.zRangeByScore(key, 0, 9999999999999d, key);
        return Response.ok(JSON.toJSONString(list));
    }

    /**
     * list size
     *
     * @return
     */
    @RequestMapping(value = "/test/redis/zset/{key}/size")
    @ParamsValidate
    public Response redisZsetSize(@PathVariable String key) {
        Long length = redisClient.zCount(key, 0, 9999999999999d);
        return Response.ok(JSON.toJSONString(length));
    }

    /**
     * 刷新基准时间差
     *
     * @return
     */
    @RequestMapping(value = "/test/reference/refush")
    @ParamsValidate
    public Response referenceValueRefush(@PathVariable String key) {
        EventFactory.calcReferenceValue();
        return Response.ok(JSON.toJSONString(EventFactory.REFERENCE_VALUE));
    }
}
